/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("xj_review_progress",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.IO;
using VOL.AppManager.IServices;
using VOL.Core.Enums;
using VOL.Core.Filters;
using VOL.Core.ManageUser;
using VOL.Core.Middleware;
using VOL.Entity.DomainModels;
using VOL.Entity.ResDto;
using VOL.Entity.新文件夹;

namespace VOL.AppManager.Controllers
{
    public partial class xj_review_progressController
    {
        private readonly Ixj_review_progressService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;
        //  private readonly KS3Client _client;
        [ActivatorUtilitiesConstructor]
        public xj_review_progressController(
            Ixj_review_progressService service,
            //   KS3Client client,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            //  _client = client;
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        [ActionLog("查询")]
        [ApiActionPermission("xj_review_progress", ActionPermissionOptions.Search)]
        [HttpPost, Route("GetData")]
        public ActionResult GetData([FromBody] xjreviewprogressQuery param)
        {
            if (UserContext.Current.RoleId == 132 || UserContext.Current.RoleId == 137 || UserContext.Current.RoleId == 133 || UserContext.Current.RoleId == 126)
            {
                param.xzqdm = UserContext.Current.XzqDm;
                param.citydm = UserContext.Current.CityDm;
                param.provcode = UserContext.Current.ProvCode;
            }
            if (param.pagesize == 0)
            {
                param.pagesize = 30;
            }

            if (param.pageindex == 0)
            {
                param.pageindex = 1;
            }
            if (param.activeName.Contains("djs"))
            {
                return JsonNormal(InvokeService("GetDsjData", new object[] { param }));
            }
            return JsonNormal(InvokeService("GetData", new object[] { param }));
        }
        [ActionLog("查询")]
        [ApiActionPermission("xj_review_progress", ActionPermissionOptions.Search)]
        [HttpPost, Route("GetDsjData")]
        public ActionResult GetDsjData([FromBody] xjreviewprogressQuery param)
        {

            if (param.pagesize == 0)
            {
                param.pagesize = 30;
            }

            if (param.pageindex == 0)
            {
                param.pageindex = 1;
            }
            return JsonNormal(InvokeService("GetDsjData", new object[] { param }));
        }
        /// <summary>
        /// 导出行政区、乡镇、村级工作进展数据
        /// </summary>
        /// <param name="pageRequest">type check</param>
        /// <returns></returns>
        [ActionLog("导出")]
        [ApiActionPermission("xj_review_progress", ActionPermissionOptions.Export)]
        [Produces("application/octet-stream")]
        [HttpPost, Route("ExportFile")]
        //[ProducesResponseType(200)]
        //[ProducesResponseType(401)]
        public IActionResult ExportExcel([FromBody] xjreviewprogressQuery param)
        {
            try
            {

                if (UserContext.Current.RoleId == 132 || UserContext.Current.RoleId == 137 || UserContext.Current.RoleId == 133 || UserContext.Current.RoleId == 126)
                {
                    param.xzqdm = UserContext.Current.XzqDm;
                    param.citydm = UserContext.Current.CityDm;
                    param.provcode = UserContext.Current.ProvCode;

                }
                PageGridData<xjreviewprogressdto> list = null;
                if (param.activeName == "dsj")
                {
                    list = InvokeService("GetDsjData", new object[] { param }) as PageGridData<xjreviewprogressdto>;
                }
                else
                {
                    list = InvokeService("GetData", new object[] { param }) as PageGridData<xjreviewprogressdto>;
                }


                var data = list.rows;
                IWorkbook wb = new XSSFWorkbook();
                ISheet sheet = wb.CreateSheet();
                // 第一行 标题
                IRow row = sheet.CreateRow(0);

                if (param.activeName == "dsj")
                {
                    row.CreateCell(0).SetCellValue("序号");
                    row.CreateCell(1).SetCellValue("地市名称");
                    row.CreateCell(2).SetCellValue("申请次数");
                    row.CreateCell(3).SetCellValue("交验次数");
                    row.CreateCell(4).SetCellValue("审核状态");
                    row.CreateCell(5).SetCellValue("审核人");
                    row.CreateCell(6).SetCellValue("审核时间");
                }
                else
                {
                    row.CreateCell(0).SetCellValue("序号");
                    row.CreateCell(1).SetCellValue("地市名称");
                    row.CreateCell(2).SetCellValue("区县名称");
                    row.CreateCell(3).SetCellValue("申请次数");
                    row.CreateCell(4).SetCellValue("交验次数");
                    row.CreateCell(5).SetCellValue("审核状态");
                    row.CreateCell(6).SetCellValue("审核人");
                    row.CreateCell(7).SetCellValue("审核时间");
                }
                // 第二行 写数据
                int i = 1;
                foreach (var item in data)
                {
                    if (item != null)
                    {
                        row = sheet.CreateRow(i);
                        if (param.activeName == "dsj")
                        {
                            row.CreateCell(0).SetCellValue(i);
                            row.CreateCell(1).SetCellValue(item.citymc);
                            row.CreateCell(2).SetCellValue(item.sqcount);
                            row.CreateCell(3).SetCellValue(item.jycs);
                            row.CreateCell(4).SetCellValue(item.shzt);
                            row.CreateCell(5).SetCellValue(item.sh_name);
                            row.CreateCell(6).SetCellValue(item.sh_time);
                        }
                        else
                        {
                            row.CreateCell(0).SetCellValue(i);
                            row.CreateCell(1).SetCellValue(item.citymc);
                            row.CreateCell(2).SetCellValue(item.xzqmc);
                            row.CreateCell(3).SetCellValue(item.sqcount);
                            row.CreateCell(4).SetCellValue(item.jycs);
                            row.CreateCell(5).SetCellValue(item.shzt);
                            row.CreateCell(6).SetCellValue(item.sh_name);
                            row.CreateCell(7).SetCellValue(item.sh_time);
                        }
                        i++;
                    }
                }
                // 写 WorkBook信息到 内存流中
                byte[] buffer = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    wb.Write(ms);
                    buffer = ms.ToArray();
                }
                return File(buffer, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", DateTime.Now.ToString("yyyyMMddHHssmm") + ".xlsx");
            }
            catch (Exception ex)
            {
                throw;
            }

        }
        private object InvokeService(string methodName, object[] parameters)
        {
            return Service.GetType().GetMethod(methodName).Invoke(Service, parameters);
        }
    }
}
